/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.simu.towercrane;

import java.io.File;
import java.util.Map;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;

import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.easemob.EMCallBack;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.simu.towercrane.domain.User;

public class DemoApplication extends Application {

	public static Context applicationContext;
	private static DemoApplication instance;
	// login user name
	public final String PREF_USERNAME = "username";
	
	/**
	 * 当前用户nickname,为了苹果推送不是userid而是昵称
	 */
	public static String currentUserNick = "";
	public static DemoHXSDKHelper hxSDKHelper = new DemoHXSDKHelper();

	@Override
	public void onCreate() {
		super.onCreate();
		/*
		 * 初始化leanClound
		 * */
		
		AVOSCloud.initialize(this, "ztxdtfdpjrzbsu3serlcvbdvyk0pfscj0uq4abwpnzzq0xjt", "3b42n9qeca6zh58r1fcd91rbblfgz24ro4boz502rl7ldms2");
		/*
		 * *end
		 */		
        applicationContext = this;
        instance = this;

        /**
         * this function will initialize the HuanXin SDK
         * 
         * @return boolean true if caller can continue to call HuanXin related APIs after calling onInit, otherwise false.
         * 
         * 环信初始化SDK帮助函数
         * 返回true如果正确初始化，否则false，如果返回为false，请在后续的调用中不要调用任何和环信相关的代码
         * 
         * for example:
         * 例子：
         * 
         * public class DemoHXSDKHelper extends HXSDKHelper
         * 
         * HXHelper = new DemoHXSDKHelper();
         * if(HXHelper.onInit(context)){
         *     // do HuanXin related work
         * }
         */
        hxSDKHelper.onInit(applicationContext);
        
        /*
         * 以下为自己的代码
         * 
         * */
       com.simu.own.utils.CrashHandler crashHandler = com.simu.own.utils.CrashHandler.getInstance();  
	     crashHandler.init(this);
        // 初始化Universal-Image-Loader
        initImageLoader();
	}	

	private void initImageLoader(){
		 File cacheDir = StorageUtils.getCacheDirectory(getApplicationContext());//图拍缓存路径/storage/sdcard0/Android/data/com.example.imageloaderdemo/cache，即该应用下的cach文件夹与  getApplicationContext().getCacheDir();等效
	        DisplayImageOptions options = new DisplayImageOptions.Builder()  
	        .showImageOnLoading(R.drawable.logo_ui)  
	        .showImageOnFail(R.drawable.logo_ui)  
	        .cacheInMemory(true)  
	        .cacheOnDisk(true)  
	        .bitmapConfig(Bitmap.Config.RGB_565)  
	        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
	        .build();
	      
	        ImageLoaderConfiguration config = new ImageLoaderConfiguration  
	        	    .Builder(getApplicationContext())  
	        	    .memoryCacheExtraOptions(480, 800) // max width, max height，即保存的每个缓存文件的最大长宽  
	        	  //  .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null) // Can slow ImageLoader, use it carefully (Better don't use it)/设置缓存的详细信息，最好不要设置这个  
	        	    .threadPoolSize(3)//线程池内加载的数量  
	        	    .threadPriority(Thread.NORM_PRIORITY - 2)  
	        	    .denyCacheImageMultipleSizesInMemory()  
	        	    .memoryCache(new WeakMemoryCache()) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现  
	        	    .memoryCacheSize(2 * 1024 * 1024)    
	        	    .discCacheSize(50 * 1024 * 1024)    
	        	    .discCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密  
	        	    .tasksProcessingOrder(QueueProcessingType.LIFO)  
	        	    //.discCacheFileCount(20) //缓存的文件数量  
	        	    .discCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径  
	        	    .defaultDisplayImageOptions(options)  
	        	    .imageDownloader(new BaseImageDownloader(getApplicationContext(), 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间  
	        	    .writeDebugLogs() // Remove for release app  
	        	    .build();//开始构建  
	        //Initialize ImageLoader with configuration.  
	        ImageLoader.getInstance().init(config);  
	}

	public static DemoApplication getInstance() {
		return instance;
	}
 
	/**
	 * 获取内存中好友user list
	 *
	 * @return
	 */
	public Map<String, User> getContactList() {
	    return hxSDKHelper.getContactList();
	}

	/**
	 * 设置好友user list到内存中
	 *
	 * @param contactList
	 */
	public void setContactList(Map<String, User> contactList) {
	    hxSDKHelper.setContactList(contactList);
	}

	/**
	 * 获取当前登陆用户名
	 *
	 * @return
	 */
	public String getUserName() {
	    return hxSDKHelper.getHXId();
	}

	/**
	 * 获取密码
	 *
	 * @return
	 */
	public String getPassword() {
		return hxSDKHelper.getPassword();
	}

	/**
	 * 设置用户名
	 *
	 * @param user
	 */
	public void setUserName(String username) {
	    hxSDKHelper.setHXId(username);
	}

	/**
	 * 设置密码 下面的实例代码 只是demo，实际的应用中需要加password 加密后存入 preference 环信sdk
	 * 内部的自动登录需要的密码，已经加密存储了
	 *
	 * @param pwd
	 */
	public void setPassword(String pwd) {
	    hxSDKHelper.setPassword(pwd);
	}

	/**
	 * 退出登录,清空数据
	 */
	public void logout(final EMCallBack emCallBack) {
		// 先调用sdk logout，在清理app中自己的数据
	    hxSDKHelper.logout(emCallBack);
	}
}
